home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Games: Greatest Hits 1996
/
Amiga Games: Greatest Hits 1996.iso
/
archive
/
userbox
/
publicdomain
/
edspell.lha
/
EdSpell
/
Utils
/
DelWord.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-28
|
4KB
|
166 lines
/************************************************************************/
/*
/* Filename : DelWord.c
/*
/* Version : 1.0
/* Date : 28 Jul 1996
/* Author : Martin Reddy <M.Reddy@ed.ac.uk>
/*
/* Purpose : Removes all entries from a file which contain a line with
/* a specified text on it. This is used in order to remove
/* entries from the user dictionary used by the ISpell spell
/* checking package.
/*
/* Usage : DelWord <word> [<UserDictFilename>]
/* Returns : 0 = word deleted successfully from the dictionary
/* 1 = word did not exist in the dictionary
/* >1 = a (disk) error occurred
/*
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* define the filename for the default user dictionary */
#define DEFAULT_USER_DICT "EdSpell:Dict/UserDict.txt"
/* the name of the temporary file to write out to */
#define TMP_FILENAME "EdSpell:Dict/UserDict.tmp"
/* some default string sizes and the standard boolean stuff */
#define MAX_FILENAME_SIZE 100
#define MAX_WORD_SIZE 100
#define TRUE 1
#define FALSE 0
/* setup the filename and search string variables for this module */
char dictFilename[MAX_FILENAME_SIZE] = DEFAULT_USER_DICT;
char searchWord[MAX_WORD_SIZE];
char version[] = "$VER: DelWord V1.0 (28/7/96)";
/*
* parseOptions: Checks the command line options for any valid options
*
*/
void parseOptions( int argc, const char *argv[] )
{
int i;
/* output a little program info if the command line args are wrong */
if ( argc < 2 || argc > 3 ) {
fprintf( stderr, "usage: DelWord <word> [<dictfile>]\n" );
fprintf( stderr, "purpose: remove a word from an ISpell user dictionary.\n" );
fprintf( stderr, "author: Martin Reddy, 28 Jul 1996. Version 1.0\n" );
exit( EXIT_FAILURE );
}
/* copy the supplied command line arguments appropriately */
strcpy( searchWord, argv[1] );
if ( argc == 3 ) strcpy( dictFilename, argv[2] );
/* ISpell entries are all in uppercase, so change search word into */
/* upper case so that we can do a case sensitive search. Also, we */
/* append a CR character for compatibility with stdio's fgets() */
for ( i = 0; i < strlen( searchWord ); ++i )
searchWord[i] = toupper( searchWord[i] );
strcat( searchWord, "\n" );
}
/*
* openFiles: open the input dictionary file and the temporary output file
*
*/
void openFiles( FILE **infile, FILE **outfile )
{
/* open the dictionary file for reading */
if ( ( *infile = fopen( dictFilename, "r" ) ) == NULL ) {
fprintf( stderr, "Cannot find user dictionary: %s\n", dictFilename );
exit( EXIT_FAILURE );
}
/* open a temporary file to write the new dictionary to */
if ( ( *outfile = fopen( TMP_FILENAME, "w" ) ) == NULL ) {
fclose( *infile );
fprintf( stderr, "Cannot create temp file: %s\n", TMP_FILENAME );
exit( EXIT_FAILURE );
}
}
/*
* main: the main program body - where it all happens!
*
*/
int main( int argc, char *argv[] )
{
FILE *infile, *outfile;
char wordBuffer[MAX_WORD_SIZE];
int deletedWords = 0;
/* parse the command line to get the word to remove from dictionary */
parseOptions( argc, argv );
/* prepare the dictionary and temporary files for reading/writing */
openFiles( &infile, &outfile );
/* go through each word in the dictionary... */
fgets( wordBuffer, MAX_WORD_SIZE, infile );
while ( feof( infile ) == FALSE ) {
/* if the word is not the one we want to remove, then output it */
/* to the temporary file that we are building. */
if ( strcmp( wordBuffer, searchWord ) != 0 ) {
if ( fprintf( outfile, wordBuffer ) != strlen( wordBuffer ) ) {
fprintf( stderr, "Error writing to %s (disk full?)\n",
TMP_FILENAME );
fclose( infile );
fclose( outfile );
remove( TMP_FILENAME );
exit( EXIT_FAILURE );
}
} else
++deletedWords;
/* then get the next word from the file... */
fgets( wordBuffer, MAX_WORD_SIZE, infile );
}
/* close all open files, overwrite the original dictionary file */
/* with the temporary one (if changed), and then exit */
fclose( infile );
fclose( outfile );
if ( deletedWords > 0 ) {
remove( dictFilename );
rename( TMP_FILENAME, dictFilename );
return EXIT_SUCCESS;
} else {
remove( TMP_FILENAME );
return 1;
}
}
/* EOF: DelWord.c */